package cc.linker.steplink.crm.merchant.service.impl;

import cc.linker.steplink.crm.merchant.dao.acs.BasicDataDao;
import cc.linker.steplink.crm.merchant.service.acs.BasicDataService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BasicDataServiceImpl implements BasicDataService {

    @Autowired
    private BasicDataDao basicDataDao;

    @Override
    public List<Map<String, Object>> getClassify() {
        return basicDataDao.getClassify();
    }

    @Override
    public List<Map<String, Object>> getCurrency(String key) {
        return basicDataDao.getCurrency(key);
    }

    @Override
    public List<Map<String, Object>> getIndustry(String key) {
        return basicDataDao.getIndustry(key);
    }

    @Override
    public List<Map<String, Object>> getProvince() {
        return basicDataDao.getProvince();
    }

    @Override
    public List<Map<String, Object>> getCity(String key) {
        return basicDataDao.getCity(key);
    }

    @Override
    public List<Map<String, Object>> getRegion(String key) {
        return basicDataDao.getRegion(key);
    }

    @Override
    public List<Map<String, Object>> getPayBank(String key, String bankId) {
        return basicDataDao.getPayBank(key, bankId);
    }

    @Override
    public List<Map<String, Object>> getBank(String key) {
        return basicDataDao.getBank(key);
    }

    @Override
    public Map<String, Object> getCommonCategory(int id) {
        Map<String, Object> commonCategory = basicDataDao.getCommonCategory(id);
        if (null == commonCategory) {
            throw GlobalException.build("参数错误");
        }
        Boolean pHaveGoods = checkCategoryGoods(id);
        commonCategory.put("haveChild", false);
        commonCategory.put("haveGoods", pHaveGoods);
        List<Map<String, Object>> sonCommonCategory = basicDataDao.getSonCommonCategory(Integer.valueOf(commonCategory.get("id").toString()));
        if (null != sonCommonCategory && sonCommonCategory.size() > 0) {
            sonCommonCategory.forEach(x -> {
                boolean isSubCategoryGoods = checkCategoryGoods(Integer.valueOf(x.get("id").toString()));
                x.put("haveGoods", isSubCategoryGoods);
                x.put("haveChild", false);
            });
            commonCategory.put("childNodes", sonCommonCategory);
            commonCategory.put("haveChild", true);
        } else {
            commonCategory.put("childNodes", new ArrayList<Map<String, Object>>());
            commonCategory.put("haveChild", false);
        }
        if (!pHaveGoods) {
            commonCategory.put("haveGoods", sonCommonCategory.size() > 0);
        }
        return commonCategory;
    }

    @Override
    public Map<String, Object> getCommonCategorys(int id) {
        Map<String, Object> commonCategory = basicDataDao.getCommonCategory(id);
        if (null == commonCategory) {
            throw GlobalException.build("参数错误");
        }
        List<Map<String, Object>> sonCommonCategory = basicDataDao.getSonCommonCategory(Integer.valueOf(commonCategory.get("id").toString()));
        if (null != sonCommonCategory && sonCommonCategory.size() > 0) {
            for (Map<String, Object> sc : sonCommonCategory) {
                int sPid = Integer.valueOf(sc.get("id").toString());
                List<Map<String, Object>> sSonCommonCategory = basicDataDao.getSonCommonCategory(sPid);
                if (null != sSonCommonCategory && sSonCommonCategory.size() > 0) {
                    for (Map<String, Object> ssc : sSonCommonCategory) {
                        boolean ssflag = false;
                        int ssPid = Integer.valueOf(ssc.get("id").toString());
                        List<Map<String, Object>> ssSonCommonCategory = basicDataDao.getSonCommonCategory(ssPid);
                        if (null != ssSonCommonCategory && ssSonCommonCategory.size() > 0) {
                            for (Map<String, Object> sssc : ssSonCommonCategory) {
                                int sssPid = Integer.valueOf(sssc.get("id").toString());
                                sssc.put("haveGoods", false);
                                sssc.put("haveChild", false);
                            }
                            ssc.put("childNodes", ssSonCommonCategory);
                            ssc.put("haveChild", true);
                        } else {
                            ssc.put("haveChild", false);
                        }
                        ssc.put("haveGoods", false);
                    }
                    sc.put("childNodes", sSonCommonCategory);
                    sc.put("haveChild", true);
                } else {
                    sc.put("haveChild", false);
                }
                sc.put("haveGoods", false);
            }
            commonCategory.put("childNodes", sonCommonCategory);
            commonCategory.put("haveChild", true);
        } else {
            commonCategory.put("haveChild", false);
        }
        commonCategory.put("haveGoods", false);
        return commonCategory;
    }

    @Override
    public Map<String, Object> getCommonCategory2(int id) {
        Map<String, Object> commonCategory = basicDataDao.getCommonCategory(id);
        if (null == commonCategory) {
            throw GlobalException.build("参数错误");
        }
        List<Map<String, Object>> sonCommonCategory = basicDataDao.getSonCommonCategory(Integer.valueOf(commonCategory.get("id").toString()));
        boolean flag = false;
        List<Map<String, Object>> childNodesList = getChildNodes(Integer.valueOf(commonCategory.get("id").toString()), sonCommonCategory);
        if (null == childNodesList || childNodesList.size() == 0) {
            flag = checkCategoryGoods(Integer.valueOf(commonCategory.get("id").toString()));
            commonCategory.put("haveChild", false);
        } else {
            commonCategory.put("haveChild", true);
            for (Map<String, Object> cl : childNodesList) {
                if (!flag) {
                    flag = (boolean) cl.get("haveGoods");
                }
            }
        }
        commonCategory.put("haveGoods", flag);
        commonCategory.put("childNodes", childNodesList);
        return commonCategory;
    }

    private List<Map<String, Object>> getChildNodes(int pid, List<Map<String, Object>> list) {
        if (null == list || list.size() == 0) {
            return new ArrayList<>();
        }
        for (Map<String, Object> da : list) {
            if (pid == Integer.valueOf(da.get("pId").toString())) {
                List<Map<String, Object>> childList = basicDataDao.getSonCommonCategory(Integer.valueOf(da.get("id").toString()));
                if (null != childList && childList.size() > 0) {
                    da.put("haveChild", true);
                    boolean flag = false;
                    for (Map<String, Object> cl : childList) {
                        boolean tempFlag = checkCategoryGoods((int) cl.get("id"));
                        if (!flag) {
                            flag = tempFlag;
                        }
                        da.put("haveGoods", flag);
                        cl.put("haveGoods", tempFlag);
                        da.put("childNodes", childList);
                        getChildNodes(Integer.valueOf(da.get("id").toString()), childList);
                    }
                } else {
                    da.put("haveChild", false);
                    da.put("haveGoods", checkCategoryGoods((int) da.get("id")));
                }
            }
        }
        return list;
    }

    private boolean checkCategoryGoods(int categoryId) {
//        return true;
        Integer categoryGoods = basicDataDao.checkCategoryGoods(categoryId);
        return categoryGoods > 0;
    }

}
